<?php defined("MAIN_DIR") or die("No direct access");

$add_error = false;
if ($route->POST('action') == "add")
{
	$link = filter_var($_POST['link'],FILTER_SANITIZE_MAGIC_QUOTES);
	$name = filter_var($_POST['name'],FILTER_SANITIZE_MAGIC_QUOTES);
	$full_name = filter_var($_POST['full_name'],FILTER_SANITIZE_MAGIC_QUOTES);
	$parent = $_POST['parent'] ? filter_var($_POST['parent_bank'],FILTER_SANITIZE_MAGIC_QUOTES) : 0;
	$address = filter_var($_POST['address'],FILTER_SANITIZE_MAGIC_QUOTES);
	$about = filter_var($_POST['about'],FILTER_SANITIZE_MAGIC_QUOTES);
	$info = trim(filter_var($_POST['info'],FILTER_SANITIZE_MAGIC_QUOTES));
	
	if ($db->getCell("select `bank_id` from `bank` where `link` = '$link' and `city_id` = '$_POST[city]' limit 1")) {
		$link_error = true;
	} else {
		
		$add_query = "insert into `bank` (`link`,`name`,`full_name`,`city_id`,`parent`,`license`,`cbrf_link`,`address`,`phone`,`site`,`about`,`info`) values ('$link','$name','$full_name','$_POST[city]','$parent','$_POST[license]','$_POST[cbrf_link]','$address','$_POST[phone]','$_POST[site]','$about','$info')";
		if (mysql_query($add_query))
		{
			$bank = mysql_fetch_array(mysql_query("select * from `bank` order by `bank_id` desc limit 1"));
			to("/admin/banks/edit/$bank[bank_id]");
		} else {
			$add_error = true;
		}
	}
}

require_once ADMIN.HEADFILE;
?>
<script type='text/javascript' src='/files/js/admin/banks.js'></script>
<div align="left">
<h1>Добавить банк</h1>
<hr /><?php
if ($add_error)
	echo "<p style='color:red'>Не удалось добавить</p>";
if (isset($link_error))
	echo "<p style='color:red'>Банк с такой ссылкой в данном городе уже присутствует</p>";
?><form method='POST'>
<input type='hidden' name='action' value='add' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td>Ссылка</td>
		<td><input style="width:200px" type='text' name='link' value='<?php echo $route->POST('link'); ?>' /> .html</td>
	</tr>
	<tr>
		<td width="150px"><input type='radio' name='parent' class='parent' value='0' <?php if (!isset($_POST['parent']) || $_POST['parent'] == '0') echo "checked='checked'"; ?> /> Головной офис</td>
		<td width="500px">
			<input type='radio' name='parent' class='parent' value='1' <?php if (isset($_POST['parent']) && $_POST['parent'] != '0') echo "checked='checked'"; ?> /> Филиал банка
			<select id='parent_bank' name='parent_bank' <?php if (!isset($_POST['parent']) || $_POST['parent'] == '0') echo "disabled='disabled'"; ?>><?php
				$main_banks_res = mysql_query("select * from `bank` where `parent` = '0' order by `name`");
				while($main_banks_row = mysql_fetch_array($main_banks_res))
				{
					echo "<option value='$main_banks_row[bank_id]'";
					if (isset($_POST['parent_bank']) && $_POST['parent_bank'] == $main_banks_row['bank_id'])
						echo " selected='selected'";
					echo ">".$main_banks_row['name']."</option>";
				}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Название</td>
		<td><input style='width:100%' type='text' name='name' id='name' value='<?php if (isset($_POST['name'])) echo $_POST['name'];?>' /></td>
	</tr>
	<tr>
		<td>Полное название</td>
		<td><input style='width:100%' type='text' name='full_name' id='fullname' value='<?php if (isset($_POST['full_name'])) echo $_POST['full_name'];?>' /></td>
	</tr>
	<tr>
		<td>Город</td>
		<td>
			<select style="width:200px" name='city'><?php
			$city_res = mysql_query("select `city_id`,`city_name` from `city` order by `city_id`");
			while ($city_row = mysql_fetch_array($city_res))
			{
				echo "<option value='$city_row[city_id]'";
				if (isset($_POST['city']) && $city_row['city_id'] == $_POST['city'])
					echo " selected='selected'";
				echo ">$city_row[city_name]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Регистрационный номер</td>
		<td><input style="width:200px" type='text' name='license' id='license' value='<?php if (isset($_POST['license'])) echo $_POST['license']?>' /></td>
	</tr>
	<tr>
		<td>Ссылка на ЦБ РФ</td>
		<td><input style="width:200px" type='text' name='cbrf_link' id='cbrf_link' value='<?php if (isset($_POST['cbrf_link'])) echo $_POST['cbrf_link']?>' /></td>
	</tr>
	<tr>
		<td>Адрес</td>
		<td><input style='width:100%' type='text' name='address' value='<?php if (isset($_POST['address'])) echo $_POST['address']?>' /></td>
	</tr>
	<tr>
		<td>Телефон</td>
		<td><input style='width:100%' type='text' name='phone' value='<?php if (isset($_POST['phone'])) echo $_POST['phone']?>' /></td>
	</tr>
	<tr>
		<td>Сайт</td>
		<td><input style='width:100%' type='text' name='site' id='site' value='<?php if (isset($_POST['site'])) echo $_POST['site'];?>' /></td>
	</tr>
	<tr valign='top'>
		<td>О банке</td>
		<td><textarea style='width:100%' rows='5' name='about' id='about'><?php if (isset($_POST['about'])) echo $_POST['about'];?></textarea></td>
	</tr>
	<tr valign='top'>
		<td>Инфо</td>
		<td><textarea style='width:100%' rows='5' name='info' id='info'><?php if (isset($_POST['info'])) echo $_POST['info'];?></textarea></td>
	</tr>
	<tr valign='top'>
		<td colspan='2'>
			<p><input type='button' id="insert_city" value='Вставить' /> - Вставить название города<br />
			<input type='button' id="insert_city_rod" value='Вставить' /> - Вставить название города в родительном падеже<br />
			<input type='button' id="insert_city_pred" value='Вставить' /> - Вставить название города в предложном падеже<br />
			</p>
		</td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Добавить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>